#![no_main]
#![no_std]

use cortex_m_rt::entry;
use periph::{gpio::PA, tim::TIM2, RCC};
use stm32f1 as _;
mod error;
mod periph;

fn tim2_init() {
    RCC.apb1enr.modify(|_, w| w.tim2en().enabled());
    TIM2.cr1.reset();
    TIM2.cr2.reset();
    TIM2.arr.write(|w| w.arr().bits(0xFFFF));
    TIM2.psc.write(|w| w.psc().bits(7200 - 1));
    TIM2.cr1.write(|w| w.cen().enabled());
}

fn delay_ms(ms: u32) {
    let count = (ms * 10) as u16;
    TIM2.cnt.write(|w| w.cnt().bits(0));
    while TIM2.cnt.read().cnt().bits() < count {}
}

// LED <=> PA2
fn led_init() {
    RCC.apb2enr.modify(|_, w| w.iopaen().enabled());
    PA.crl.modify(|_, w| w.mode2().output50().cnf2().push_pull());
}

#[allow(non_upper_case_globals, non_snake_case, dead_code, non_camel_case_types)]
mod c_src {
    include!("../test.rs");
}

#[entry]
fn main() -> ! {

    unsafe {
        c_src::SystemInit();
    }

    tim2_init();
    led_init();
    unsafe {
        // 测试调用 c 代码
        let _s = c_src::add(1, 2);
        let _n = c_src::my_abs(21, 32);
    };
    loop {
        PA.odr.modify(|r, w| {
            w.odr2().bit(!r.odr2().bit())
        });
        delay_ms(100);
    }
}




